#include "Animation.h"
#include "Debug.h"

Animation::~Animation(void)
{
}

Animation::Animation( void ): m_delay(100), m_visualPack(NULL), m_diff(0), m_currentVisual(VISUAL_TYPE_NONE),
							  m_currentFrame(0), m_currentAnimation(0),m_paused(false)
{

}
Animation::Animation( VisualPack* visual ) 
{
	m_visualPack = visual;
}

void Animation::Update( uint16 diff )
{
	if(!m_visualPack || m_paused) return;

	m_diff += diff;
	if(m_diff >= m_delay) {
		++m_currentFrame;
		if(m_currentFrame >= m_visualPack->GetVisual(m_currentVisual)->GetFrames())
			m_currentFrame = 0;

		m_diff = 0;
	} 
}

const Rect& Animation::GetRect()
{
	if(!m_visualPack) {
		sLog.outError("Animation::GetRect(): called but there is no visual pack chosen! CRASH ALERT!");
		return m_rect;
	}
	uint16 width = 0;	 uint16 xpos = 0;
	uint16 height = 0;	 uint16 ypos = 0;

	width  = m_visualPack->GetVisual(m_currentVisual)->GetFrameWidth();
	height = m_visualPack->GetVisual(m_currentVisual)->GetFrameHeight(); 

	switch(m_visualPack->GetType())
	{
	case VISUAL_TYPE_NONE:
		break;
	case VISUAL_TYPE_BUILDING:
		//should only have one frame.. so 0 x offset and 0 y offset
		break;
	case VISUAL_TYPE_UNIT:
		xpos = m_currentFrame*width;
		ypos = m_currentAnimation*height;
		break;

	default: 
		sLog.outError("Animation::GetRect(): No such visual type! %u", m_visualPack->GetType());
	}

	m_rect.x = xpos;	 m_rect.y = ypos;
	m_rect.w = width;	 m_rect.h = height;

	return m_rect;
}